package com.hub.service.asset.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.util.StringUtil;
import com.hub.service.impl.BaseServiceImpl;
import com.hub.dto.asset.RegionInfoDto;
import com.hub.dto.asset.SearchRegionInfoDto;
import com.hub.entity.asset.RegionInfoEntity;
import com.hub.frame.consts.ConstantUtil;
import com.hub.mapper.asset.RegionInfoMapper;
import com.hub.model.asset.RegionInfoModel;
import com.hub.service.asset.IRegionInfoService;
import com.hub.utils.ApiCodeEnum;
import com.hub.utils.ApiResultObject;
import com.hub.utils.PagingResultObject;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RegionInfoServiceImpl extends BaseServiceImpl<RegionInfoMapper,RegionInfoEntity> implements IRegionInfoService {
    @Autowired
    private RegionInfoMapper regionInfoMapper;
    @Autowired
    private Mapper mapper;

    /**
     * 
     * 获取区域列表
     * @param queryModel
     * @return
     */
    public PagingResultObject getList(SearchRegionInfoDto queryModel){
        queryModel.setPageIndex( queryModel.getPageIndex() > 0 ? queryModel.getPageIndex() : 1);
        queryModel.setPageSize(queryModel.getPageSize() > 0?queryModel.getPageSize():20);
        //查询条件
        QueryWrapper<RegionInfoEntity> wrapper = new QueryWrapper<>();
        if(StringUtil.isNotEmpty(queryModel.getRegionName())){
            wrapper.like("region_name",queryModel.getRegionName());
        }
        wrapper.eq("active_flg", ConstantUtil.YES);
        //查出所有父类
        wrapper.eq("parent_id",0);
        IPage<RegionInfoEntity> parentList = baseMapper.selectPage(new Page<>(queryModel.getPageIndex(), queryModel.getPageSize()),wrapper);
        List<RegionInfoModel> modelList = new ArrayList<>();
        //遍历父级
        for(RegionInfoEntity entity : parentList.getRecords()){
            RegionInfoModel model = mapper.map(entity,RegionInfoModel.class);
            //查询子类
            QueryWrapper sonWrapper = new QueryWrapper<>();
            sonWrapper.eq("parent_id", entity.getId());
            sonWrapper.eq("active_flg", ConstantUtil.YES);
            List<RegionInfoModel> sonList = baseMapper.selectList(sonWrapper);
            //判断是否存在子类
            if(sonList.size() > 0){
                model.setChildren(sonList);
            }
            modelList.add(model);
        }
        return PagingResultObject.success(modelList,queryModel.getPageIndex(),queryModel.getPageSize(),parentList.getTotal());
    }

    /**
     * 
     * 新增区域
     * @param addModel
     * @return
     */
    public ApiResultObject addRgion(RegionInfoDto addModel){
        //判断区域是否已存在
        QueryWrapper<RegionInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("region_name",addModel.getRegionName());
        wrapper.eq("active_flg", ConstantUtil.YES);
        if(this.count(wrapper) > 0){
            return ApiResultObject.failed("该区域已存在，无法保存,请重新输入！");
        }
        //新增
        RegionInfoEntity entity = mapper.map(addModel,RegionInfoEntity.class);
        if(this.save(entity)){
            return ApiResultObject.success(ApiCodeEnum.SUCCESS.getCode(),"添加成功！");
        }
        return ApiResultObject.failed(ApiCodeEnum.FAILED.getCode(),"添加失败！");
    }

    /**
     * 
     * 修改区域
     * @param modifyModel
     * @return
     */
    public ApiResultObject modifyRegion(RegionInfoDto modifyModel){
        //判断区域是否一存在（不包含自身）
        QueryWrapper<RegionInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("region_name",modifyModel.getRegionName());
        wrapper.ne("id",modifyModel.getId());
        wrapper.eq("active_flg", ConstantUtil.YES);
        if(this.count(wrapper) > 0){
            return ApiResultObject.failed("该区域已存在，无法保存,请重新输入！");
        }
        //修改
        RegionInfoEntity entity = mapper.map(modifyModel,RegionInfoEntity.class);
        if(this.updateById(entity)){
            return ApiResultObject.success(ApiCodeEnum.SUCCESS.getCode(),"修改成功！");
        }
        return ApiResultObject.success(ApiCodeEnum.SUCCESS.getCode(),"修改失败！");
    }

    /**
     * 
     * 删除区域
     * @param id
     * @return
     */
    public ApiResultObject deleteRegion(int id){
        if(id < 0){
            return ApiResultObject.failed(ApiCodeEnum.FAILED.getCode(),"参数异常: 资产信息ID异常!");
        }
        //查询是否有子类
        QueryWrapper<RegionInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        wrapper.eq("active_flg", ConstantUtil.YES);
        int count = baseMapper.selectCount(wrapper);
        if(count > 0){
            return ApiResultObject.failed(ApiCodeEnum.FAILED.getCode(),"此区域下含有子级区域，无法删除！");
        }
        if(this.removeById(id)){
            return ApiResultObject.failed(ApiCodeEnum.SUCCESS.getCode(),"删除成功！");
        }
        return ApiResultObject.failed(ApiCodeEnum.FAILED.getCode(),"删除失败！");
    }
}
